<?php
namespace ServicesApi\Request;
class AddEditGoodsRequest{
    /**
     * 商品验证
     * @param $data
     */
    public function goodsVerification($data)
    {
        if (!isset($data['name']) || empty($data['name'])) {
            return ['status'=>false,'msg'=>'商品名称不能为空'];
        }
        if (!isset($data['category_id']) || empty($data['category_id'])) {
            return ['status'=>false,'msg'=>'请选择商品分类'];
        }else{
            if(count($data['category_id']) != 3){
                return ['status'=>false,'msg'=>'必须选择三级分类'];
            }
        }
        if (!isset($data['detailed_img']) || empty($data['detailed_img'])) {
            return ['status'=>false,'msg'=>'请上传图片轮播图'];
        }
        if(!isset($data['sku']) || empty($data['sku'])){
            return ['status'=>false,'msg'=>'请选择价格/规格参数'];
        }else{
            foreach ($data['sku'] as $key=>$value){
                if($value['price'] <= $value['endprice']){
                    return ['status'=>false,'msg'=>'商品原价必须大于折扣价'];
                }
                if($value['endprice'] <= $value['commission']){
                    return ['status'=>false,'msg'=>'佣金价格必须小于折扣价'];
                }
                if(!isset($value['image_url']) || empty($value['image_url'])){
                    return ['status'=>false,'msg'=>'价格/规格参数不能为空'];
                }
            }

        }
        return ['status'=>true,'msg'=>'参数校验正确'];
    }


    private function skuVerification($val)
    {
        $val['value_names'] = str_replace('<br/>', '', $val['value_names']);
        if (!isset($val['value_ids']) || empty($val['value_ids'])) {
            return '缺少value_ids';
        }
        if (!isset($val['imageUrl']) || empty($val['imageUrl'])) {
            return $val['value_names'] . '缺少图片';
        }
        if (!isset($val['stock']) || empty($val['stock'])) {
            return $val['value_names'] . '缺少库存数';
        }
        if (!isset($val['price']) || empty($val['price'])) {
            return $val['value_names'] . '缺少成本价';
        }
        if (!isset($val['endprice']) || empty($val['endprice'])) {
            return $val['value_names'] . '缺少购买价';
        }
        return true;
    }
}